package com.ruoyi.workorder.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.threadlocal.UserThreadLocalUtils;
import com.ruoyi.workorder.domain.SubmitReplyDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.workorder.domain.WorkOrder;
import com.ruoyi.workorder.service.IWorkOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工单管理Controller
 * 
 * @author ruoyi
 * @date 2023-06-01
 */
@RestController
@RequestMapping("/ruoyi-work-order/workorder")
public class WorkOrderController extends BaseController
{
    @Autowired
    private IWorkOrderService workOrderService;

    /**
     * 查询工单管理列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-work-order:workorder:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkOrder workOrder)
    {
        startPage();
        List<WorkOrder> list = workOrderService.selectWorkOrderList(workOrder);
        return getDataTable(list);
    }

    /**
     * 导出工单管理列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-work-order:workorder:export')")
    @Log(title = "工单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkOrder workOrder)
    {
        List<WorkOrder> list = workOrderService.selectWorkOrderList(workOrder);
        ExcelUtil<WorkOrder> util = new ExcelUtil<WorkOrder>(WorkOrder.class);
        util.exportExcel(response, list, "工单管理数据");
    }

    /**
     * 获取工单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-work-order:workorder:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(workOrderService.selectWorkOrderById(id));
    }

    /**
     * 新增工单管理
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-work-order:workorder:add')")
    @Log(title = "工单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkOrder workOrder)
    {
        return toAjax(workOrderService.insertWorkOrder(workOrder));
    }

    /**
     * 修改工单管理
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-work-order:workorder:edit')")
    @Log(title = "工单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkOrder workOrder)
    {
        return toAjax(workOrderService.updateWorkOrder(workOrder));
    }

    /**
     * 删除工单管理
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-work-order:workorder:remove')")
    @Log(title = "工单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(workOrderService.deleteWorkOrderByIds(ids));
    }
    
    /**
     * 工单管理派单
     */
    @Anonymous
    @GetMapping("/receivingOrder/{id}/{dispatchName}")
    public AjaxResult receivingOrder(@PathVariable("id") Long id, @PathVariable("dispatchName") String dispatchName){
        //调用service层的派单方法
        return workOrderService.receivingOrder(id,dispatchName);
    }
    
    /**
     * 提交回复
     */
    @Anonymous
    @PostMapping("/submitReply")
    public AjaxResult submitReply(@RequestBody SubmitReplyDTO dto){
        //调用service层提交回复方法
        return workOrderService.submitReply(dto);
    }
    
    /**
     * 获取当前登录的用户名
     * @return
     */
    @Anonymous
    @GetMapping("/getUserName")
    public AjaxResult getUserName(){
        //返回存放在ThreadLocal内的用户名
        System.out.println("获取用户名");
        return AjaxResult.success(UserThreadLocalUtils.getUserName());
    }
}
